package com.fastadmin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.mapper.impl.FUserMapper;
import com.fastadmin.pojo.FUser;
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequiredArgsConstructor
@Slf4j
public class FUserController extends BaseController {

    private final FUserMapper fuserMapper;

    @GetMapping("sys/fuser/index")
    public String index() {
        return "sys/fuser/index";
    }

    @GetMapping("sys/fuser/add")
    public String add() {
        return "sys/fuser/add";
    }

    @GetMapping("sys/fuser/edit")
    public String index(@RequestParam Long ids, Model model) {
        FUser fuser = this.fuserMapper.findById(ids);
        model.addAttribute("fuser", fuser);
        return "sys/fuser/edit";
    }

    @RequestMapping("sys/fuser/data")
    @ResponseBody
    public Object data(FastParam fastParam) {
        PageInfo<FUser> pageInfo = this.fuserMapper.findByFastParam(fastParam);
        return datagrid(pageInfo);
    }

    @RequestMapping("sys/fuser/del")
    @ResponseBody
    public Object del(String ids) {
        String[] split = StrUtil.split(ids, ",");
        for (String s : split) {
            fuserMapper.deleteById(s);
        }
        return ok();
    }

    @RequestMapping("sys/fuser/save")
    @ResponseBody
    public Object save(FUser fuser) {
        if (ObjectUtil.isNotNull(fuser.getId())) {
            //修改
            fuserMapper.update(fuser);
        } else {
            //保存
            fuserMapper.save(fuser);
        }
        return ok();
    }

}
